package com.xiaoshuidi.cloud.module.report.xxljob;

import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.module.report.dal.DO.ContractHouseOwnerDO;
import com.xiaoshuidi.cloud.module.report.dal.DO.ContractRentorderDO;
import com.xiaoshuidi.cloud.module.report.dal.DO.RoomHouseDO;
import com.xiaoshuidi.cloud.module.report.dal.DO.report.ReportOwnerDepositDO;
import com.xiaoshuidi.cloud.module.report.dal.mapper.ContractHouseOwnerMapper;
import com.xiaoshuidi.cloud.module.report.dal.mapper.ContractRentorderMapper;
import com.xiaoshuidi.cloud.module.report.dal.mapper.RoomHouseMapper;
import com.xiaoshuidi.cloud.module.report.service.finance.DepositOwnerService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 业主押金报表
 */
@Component
@Slf4j
public class DepositOwnerHandleJob {

    @Resource
    private ContractHouseOwnerMapper contractHouseOwnerMapper;
    @Resource
    private RoomHouseMapper roomHouseMapper;
    @Resource
    private DepositOwnerService depositOwnerService;
    @Resource
    private ContractRentorderMapper contractRentorderMapper;

    @XxlJob("depositOwnerHandleJob")
    @TenantIgnore
    public void execute() {
        //获取押金报表流出押金的最大订单id
        List<ReportOwnerDepositDO> list= depositOwnerService.selectListUnHandel();
        if(CollectionUtils.isEmpty(list)){
            log.info("没有需要处理的业主押金明细");
            return;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (ReportOwnerDepositDO e : list) {
            try {
                //查询账单
                ContractRentorderDO rentorderDO = contractRentorderMapper.selectById(e.getOrderId());
                if(rentorderDO==null||rentorderDO.getBusinessId()==null){
                    log.error("===脏数据此账单不应该为空 脏数据 orderId:{}",e.getOrderId());
                    continue;
                }
                //查询合同
                ContractHouseOwnerDO houseOwnerDO = contractHouseOwnerMapper.selectById(rentorderDO.getBusinessId());
                if(houseOwnerDO ==null){
                    log.error("====脏数据 有业主合同id没业主合同 contractId:{}",rentorderDO.getBusinessId());
                    continue;
                }
                //查询房源信息
                RoomHouseDO houseDO = roomHouseMapper.selectById(rentorderDO.getRoomId());
                if(houseDO ==null){
                    log.error("====脏数据 有房间id无房源数据 roomId:{}",rentorderDO.getRoomId());
                    continue;
                }
                //拼凑需要数据
                e.setRoomId(rentorderDO.getRoomId());
                String contractPeriod = houseOwnerDO.getStartTime().format(formatter) + "~" + houseOwnerDO.getEndTime().format(formatter);
                e.setContractPeriod(contractPeriod);
                e.setOwnerName(houseOwnerDO.getOwnerName());
                e.setOwnerPhone(houseOwnerDO.getPhone());
                e.setPayMethod("银行转账");
                e.setCreateTime(houseOwnerDO.getCreateTime());
                e.setHandleFlag(1);
                //集中式和分散式拼凑方法不一样
                StringBuilder houseName = new StringBuilder(houseDO.getArea()).append(houseDO.getBuildingNo()).append("栋");
                if (houseDO.getIsConcentrated()) {
                    //集中式 小区名-楼栋-房源名称   room_house.area - room_house.building_no栋 - room_house.name
                    e.setHouseResource(houseName.append(houseDO.getName()).toString());
                } else {
                    //分散式  小区名-楼栋-单元-房源名称 room_house.area - room_house.building_no栋 - room_house.unit_no单元 - room_house.name
                    e.setHouseResource(houseName.append(houseDO.getUnitNo()).append("单元").append(houseDO.getName()).toString());
                }
            } catch (Exception ex) {
                log.error("=====处理押金报表异常 orderId:{}",e.getOrderId());
                ex.printStackTrace();
            }
        }
        if(CollectionUtils.isNotEmpty(list)){
            depositOwnerService.updateBatch(list);
        }
        //删除续租合同押金 押金只应该算在首次租房时
        List<Long> renewalIds = contractHouseOwnerMapper.selectRenewalIds();
        List<Long> orderIds = contractRentorderMapper.selectOwnerRenewalOrderIds(renewalIds);
        depositOwnerService.updateByOrderIds(orderIds);
    }

}